package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.bean.SysCategory;
import com.qf.dto.R;
import com.qf.service.SysCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 *  @author Administrator
 *  @version V1.0
 *  @Project smartdeviceplatgorm
 *  @Package com.qf.controller
 *  @Description
 *  @Date2022/3/17 0017 18:42
 */
@RestController
@RequestMapping("/category")
public class SysCategoryController {
    private SysCategoryService sysCategoryService;
    @Autowired
    public void setSysCategoryService(SysCategoryService sysCategoryService) {
        this.sysCategoryService = sysCategoryService;
    }

    @GetMapping("categroy/{id}")
    public R findById(@PathVariable Integer id) {
        SysCategory sysCategory = sysCategoryService.findById(id);
        return R.setOk(sysCategory);
    }
    @PostMapping("/add")
    public R addSysCategory(@RequestBody SysCategory category) {
        sysCategoryService.addSysCategory(category);
        return R.setOk();
    }
    @PostMapping("/categories")
    public R findAllByPage(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int limit, String categoryName) {
        PageInfo<SysCategory> pageInfo = sysCategoryService.findByPage(page, limit, categoryName);
        long total = pageInfo.getTotal();
        List<SysCategory> sysCategoryList=pageInfo.getList();
        return R.getR(0,"success",sysCategoryList,total);
    }

    @PostMapping("/update")
    public R updateSysCategory(@RequestBody SysCategory category) {
        sysCategoryService.updateSysCategory(category);
        return R.setOk();
    }
    @PostMapping("/delete")
    public R deleteSysCategory(@RequestBody List<Integer> ids) {
        int count = sysCategoryService.deleteByIds(ids);
        return R.setOk(count);
    }

}
